<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Animated Drop Down Menu with jQuery and CSS</title>
<style type="text/css">
body{font-family:Arial, Helvetica, sans-serif; font-size:12px;}
pre{font-size:11px;}
</style>
</head>

<body>
<h1>Animated Drop Down using jQuery and CSS</h1>
<p><img src="preview.png" alt="preview" width="200" height="200" /></p>
<p><a href="example.html">View the effect</a></p>
<p>Drop down menus are a really convient way to fit a large menu into a really small initial space. For a long time people have just used a form element for standard drop downs, but with minimal effort you can create a much slicker effect using jQuery and CSS.</p>
<h3>Step 1: The HTML</h3>
<p>Before we can do anything, we need to link our CSS file and our jQuery file in the header our of HTML document:</p>
<pre name="code" class="html">&lt;link href=&quot;css/style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;<br />
&lt;script type=&quot;text/javascript&quot; src=&quot;jsfiles/jquery.js&quot;&gt;&lt;/script&gt;</pre>
<p>These two files will contain our styles and the javascript effect library (duh), but we can style or animate anything, we need to build the list itself. We are going to use a simple unordered list:</p>
<pre name="code" class="html">&lt;ul class=&quot;menu_body&quot;&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About Us&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Portfolio&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Blog&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Support Forums&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Gallery&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact Us&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</pre>
<p>What we have here is as simple as it looks. We have an unordered list with the class of &quot;menu_body&quot;. Inside we have multiple list items, each containing a navigation link. Next we need to add an image above the list. This image will serve as the list's heading an all that is visible when the drop down is collapsed. It doesn't have to be an image, this would work exactly the same with text to launch and collapse the menu, I just wanted something visual. If you want to use mine, you can find it <a href="images/navigate.png" target="_blank">here</a>. With the image added we have our complete HTML:</p>
<pre name="code" class="html">&lt;img src=&quot;images/navigate.png&quot; width=&quot;184&quot; height=&quot;32&quot; class=&quot;menu_head&quot; /&gt;<br />
  &lt;ul class=&quot;menu_body&quot;&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About Us&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Portfolio&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Blog&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Support Forums&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Gallery&lt;/a&gt;&lt;/li&gt;<br />
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact Us&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</pre>
<p>We need to give our image a class name, so we have something to reference by when we start our jQuery. I used the class &quot;menu_head&quot;. Here is what we have so far, a completely unstyled list with an image on top:</p>
<p><img src="example_images/1.png" alt="Unstyled List" width="184" height="193" /></p>
<h3>Step 2: The CSS</h3>
<p>Next we need to give our list some style. First, lets do our top level styling:</p>
<pre name="code" class="css">body{background:#534741;font-family:Arial, Helvetica, sans-serif; font-size:12px;}<br />
ul, li{margin:0; padding:0; list-style:none;}</pre>
<p>Nothing too comples here, just setting a background color, font, and font size. Also, we are telling the list to have no padding, margin, or bullets. Now our list is just a heading and a neat column of links:</p>
<p><img src="example_images/2.png" alt="Some Style" width="184" height="193" /></p>
<p>Now we can style the heading image and each list item. Here is the CSS:</p>

<pre name="code" class="css">
.menu_head{border:1px solid #998675;}
.menu_body {width:184px;border-right:1px solid #998675;border-bottom:1px solid #998675;border-left:1px solid #998675;}<br />
.menu_body li{background:#493e3b;}<br />
.menu_body li a{color:#FFFFFF; text-decoration:none; padding:10px; display:block;}</pre>
<p>Here's what we just did. We added a light tan border around the image &quot;menu_head&quot;. On the unordered list &quot;menu_body&quot; we set a width (the same width as our image), and we added a light tan border to every side but the top (there will already be a line there since we have one around our image). We set a background color for each list item and we styled each link. Every link is now white, not underlines, has a nice amount of padding, and is set to a block display (this will make the whole box around the link clickable, not just the text itself).</p>
<p>Here is what our list should look now:</p>
<p><img src="example_images/3.png" alt="More Style" width="188" height="282" /></p>
<h3>Step 3: The jQuery</h3>
<p>Our first step will be to add the jQuery to tell the list items to alternate their background colors. In the head of your HTML document, add:</p>
<pre name="code" class="js">&lt;script type=&quot;text/javascript&quot;&gt;<br />
$(document).ready(function () {<br />$(&quot;ul.menu_body li:even&quot;).addClass(&quot;alt&quot;);<br />
});<br />
&lt;/script&gt;</pre>
<p>This is a basic jQuery function. When the document is ready, the function will add a special class of &quot;alt&quot; to each alternating row of our list. With the new classes applied, we can add a new CSS rule for the class &quot;alt&quot;</p>
<pre name="code" class="css">.menu_body li.alt{background:#362f2d;}</pre>
<p>Now the rows will alternate between lighter and darker shades of brown, like this:</p>
<p><img src="example_images/4.png" alt="Row colors" width="188" height="282" /></p>
<p>Now the the list has the overall look that we want, we can go ahead and completely hide it with CSS. In the CSS rule for &quot;menu_body&quot; add the property &quot;display:none;&quot; like this:</p>
<pre name="code" class="css">.menu_body {display:none; width:184px;border-right:1px solid #998675;border-bottom:1px solid #998675;border-left:1px solid #998675;}</pre>
<p>If you look at the page now, all you should be able to see if the heading image. For now, the menu has seemingly dissappeared. Time to bring it back with jQuery:</p>
<pre name="code" class="js">&lt;script type=&quot;text/javascript&quot;&gt;<br />
$(document).ready(function () {<br />
$(&quot;ul.menu_body li:even&quot;).addClass(&quot;alt&quot;);<br />
<br />
$('img.menu_head').click(function () {<br />
$('ul.menu_body').slideToggle('medium');<br />
  <br />
});<br />
});<br />
&lt;/script&gt;</pre>
<p>We added a new function that runs when the image with the class of &quot;menu_head&quot; is clicked. A click is just one of the <a href="http://docs.jquery.com/Events" target="_blank">events</a> jQuery recognizes. You could also have the function run on mouseover, a key press, or numerous other things. When a click event is registered, jQuery will use the effect slideToggle on the unordered list with the class of &quot;menu_body&quot;. jQuery has a large list of <a href="http://docs.jquery.com/Effects" target="_blank">effects</a> and various effect plugins, there is really no limit on how you can animate the list sliding open and closed. slideToggle allows you to set a speed, I chose medium, but you can also use &quot;fast&quot;, &quot;slow&quot;, or define a number in miliseconds.</p>
<p>The menu should now slide open and closed when you click on the heading image:</p>
<p><img src="example_images/5.png" alt="Animation" width="599" height="282" /></p>
<p>You could stop now and have a pretty decent animated menu, but with jQuery its very easy to add simple hover effects. First, we need to add some to our CSS:</p>
<pre name="code" class="css">.menu_body li a:hover{padding:15px 10px; font-weight:bold;}</pre>
<p>This will do two things. When one of our links is hovered over, the padding on the top and bottom will be expanded to 15px (making each rolled over area taller and allowing the shift) and chaning the font weight to bold.</p>
<p>Next we can add some jQuery animation:</p>
<pre name="code" class="js">&lt;script type=&quot;text/javascript&quot;&gt;<br />
$(document).ready(function () {<br />
$(&quot;ul.menu_body li:even&quot;).addClass(&quot;alt&quot;);<br />
$('img.menu_head').click(function () {<br />
$('ul.menu_body').slideToggle('medium');<br />
});
<br />
$('ul.menu_body li a').mouseover(function () {<br />
$(this).animate({ fontSize: &quot;14px&quot;, paddingLeft: &quot;20px&quot; }, 50 );<br />
});
<br />
});<br />
&lt;/script&gt;</pre>
<p>We've now added a mouseover function, one that runs anytime you mouseover any list item's link within our unordered list. The function is set to run on &quot;this&quot; which just means the element will run the function on itself. We are using the jQuery effect <a href="http://docs.jquery.com/Effects/animate#paramsdurationeasingcallback" target="_blank">animate</a>, which allows for many different parameters, along with a duration of time to run them in. We've told the function to change the font size to 14px, change the padding-left to 20px, and to do both things in 50 miliseconds.</p>
<p>At this point, the menu can expand and collapse, and the mouseover effect on the links should be working. Now we just need to tell it to reverse the effect when we mouseoff a link. If we don't tell it to reset each animation, the links will &quot;stick&quot; in their new font size and position. We, of course, want each link to snap back to its original position. We can easily do this like so:</p>
<pre name="code" class="js">&lt;script type=&quot;text/javascript&quot;&gt;<br />
$(document).ready(function () {<br />
$(&quot;ul.menu_body li:even&quot;).addClass(&quot;alt&quot;);<br />
$('img.menu_head').click(function () {<br />
$('ul.menu_body').slideToggle('medium');<br />
});<br /><br />
$('ul.menu_body li a').mouseover(function () {<br />
$(this).animate({ fontSize: &quot;14px&quot;, paddingLeft: &quot;20px&quot; }, 50 );<br />
});<br /><br />
$('ul.menu_body li a').mouseout.(function () {<br />
$(this).animate({ fontSize: &quot;12px&quot;, paddingLeft: &quot;10px&quot; }, 50 );<br />
});<br />
});<br />
&lt;/script&gt;</pre>
<p>This is our last bit of jQuery. It is a function that runs whenever you mouseout of a list item's link. It will reset the font to the original size (12px), change the padding-left back (to 10px). It will do both things in 50 miliseconds, the same speed as the first half of the animation.<br />
</p>
<p>Your menu should now be fully functional. It should be able to click open and close (in a sliding animation) and each link should animate when touched with the mouse (expanding the height, text size, and left-margin). I purposely tried to keep this menu light on images, but this simple effect is really easy to dress up some. For example, each link's hover state could have an image background. Each image could be specific to each section for a really polished, intuituve menu and it would all fit into a small rectangle when not in use.</p>
<p>You can see the finished menu here.</p>
</body>
</html>
